package com.juntuo.controller;

import com.github.pagehelper.PageInfo;
import com.juntuo.Enums.ParkStatus;
import com.juntuo.entity.Company;
import com.juntuo.entity.Device;
import com.juntuo.entity.Park;
import com.juntuo.entity.User;
import com.juntuo.platformUtils.PostAsynCommandV4Util;
import com.juntuo.service.CompanyService;
import com.juntuo.service.DeviceService;
import com.juntuo.service.ParkService;
import com.juntuo.shiro.ShiroUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 对设备等进行管理
 * @author chenxj
 * @create 2017/03/03 11:04
 */
@Controller
@RequestMapping("/device")
public class DeviceController {
    Logger log=Logger.getLogger(DeviceController.class);
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ParkService parkService;
    @Autowired
    private CompanyService companyService;
    @RequestMapping("/index")
    public String toIndex(Model model){
        User user = ShiroUtil.currentUser();

        if(user.getCompanyId()!=null){
            model.addAttribute("userType","company");
        }else{
            model.addAttribute("userType","admin");
        }
        //todo  这里只是测试用的
        if(user.getCompanyId()!=null && user.getCompanyId()==8){
            return "/device/index4test";
        }
        return "/device/index";
    }

    @RequestMapping("/page")
    @ResponseBody
    public PageInfo<Map<String,Object>> getAllList(int pageNumber, int pageSize, String province,String city, String district,String companyName,String parkNumber, String name){
        User user = ShiroUtil.currentUser();
        if(user.getCompanyId()!=null){
            Company company = new Company();
            company.setId(user.getCompanyId());
            company=companyService.selectOne(company);
            companyName=company.getName();
        }
        return deviceService.findPagedDevice(pageNumber,pageSize,province,city,district,companyName,parkNumber,name);
//        return null;
    }


    /**
     * 暂时只做修改 不新增
     * @param device
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public int save(Device device){
        if(device.getId()!=null){
            deviceService.updateByPrimaryKeySelective(device);
        }
        return 1;
    }

    /**
     * 暂时只做修改 不新增
     * @param
     * @return
     */
    @RequestMapping("/command/{deviceId}")
    @ResponseBody
    public int command(@PathVariable String  deviceId) {
            try {
                PostAsynCommandV4Util.command(deviceId);
                return 1;
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
    }
//    @RequestMapping("/delete/{id}")
//    @ResponseBody
//    public int deleteOne(@PathVariable int id){
//        Device de= new Device();
//        de.setId(id);
//        Device device = deviceService.selectOne(de);
//        if(device.getBindTime()!=null){
//            return 0;
//        }
//        deviceService.delete(device);
//        return 1;
//    }

//    @RequestMapping("/bind")
//    @ResponseBody
//    public int bindDevice(int id,int parkId){
//        Device de= new Device();
//        de.setId(id);
//        de.setParkId(parkId);
//        de.setBindTime(new Date());
//        deviceService.updateByPrimaryKeySelective(de);
//        Park park = new Park();
//        park.setId(parkId);
//        park.setStatus(ParkStatus.BINDED.getCode());
//        parkService.updateByPrimaryKeySelective(park);
//        return 1;
//    }
//
//    @RequestMapping("/unbind")
//    @ResponseBody
//    public int unbindDevice(int id){
//        Device de= new Device();
//        de.setId(id);
//        Device device = deviceService.selectOne(de);
//        int parkId = device.getParkId();
//        Park park = new Park();
//        park.setId(parkId);
//        park.setStatus(ParkStatus.WAITING_FOR_BIND.getCode());
//        parkService.updateByPrimaryKeySelective(park);
//        device.setParkId(null);
//        device.setBindTime(null);
//        deviceService.updateByPrimaryKey(device);
//        return 1;
//    }



}
